package com.weme.common.util;

import android.util.Log;


import com.weme.common.BaseApplication;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;

import okhttp3.ResponseBody;

public class FileUtils {
    public static String createDir( String filename) {
        File futureStudioIconFile = new File(fileUrl+ filename);
        if (!futureStudioIconFile.exists())
        // 若不存在，创建目录，可以在应用启动的时候创建
        {
            futureStudioIconFile.mkdirs();
        }
        return fileUrl+ filename;
    }

    public static String fileUrl = BaseApplication.sInstance.getExternalCacheDir().getAbsolutePath() +  "/gift/" ;


    public static boolean writeResponseBodyToDisk(ResponseBody body, String savaName) {
        try {
            File futureStudioIconFile = new File(fileUrl+ savaName);
            Log.e("dow","保存地址"+fileUrl+ savaName);
            createDir(fileUrl+"gift");
            InputStream inputStream = null;
            OutputStream outputStream = null;

            try {
                byte[] fileReader = new byte[4096];

                long fileSize = body.contentLength();
                long fileSizeDownloaded = 0;

                inputStream = body.byteStream();
                outputStream = new FileOutputStream(futureStudioIconFile);

                while (true) {
                    int read = inputStream.read(fileReader);
                    if (read == -1) {
                        Log.e("dow", "文件大小是0");
                        break;
                    }
                    outputStream.write(fileReader, 0, read);
                    fileSizeDownloaded += read;
                    Log.e("dow", "file download: " + fileSizeDownloaded + " of " + fileSize);
                }

                outputStream.flush();
                return true;
            } catch (IOException e) {
                Log.e("dow", "保存失败"+e.getMessage());
                return false;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }
            }
        } catch (IOException e) {
            Log.e("dow", "保存失败"+e.getMessage());
            return false;
        }
    }

    public static boolean deleteFile(File file) {

        if (file.exists()) {
            return file.delete();
        } else {
            return false;
        }
    }
    public static boolean isFileExit(String fileName){
        File file = new File(fileName);
        return file.exists();
    }

    public static String calculateMD5(File file) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            InputStream is = new FileInputStream(file);
            byte[] buffer = new byte[8192];
            int read;
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            StringBuilder result = new StringBuilder();
            for (byte b : md5sum) {
                result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static boolean fileIsExists(String fileName) {
        try {
            File f=new File(fileName);
            if(f.exists()) {
                Log.e("tag","没有"+fileName);
                return true;
            }else{
                Log.e("tag","有"+fileName);
                return false;
            }
        } catch (Exception e) {
            Log.e("tag","错误"+fileName);
            return false;
        }
    }
}
